{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression, SGDRegressor\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import mean_squared_error\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "data": {
      "text/plain": "        CRIM    ZN  INDUS  CHAS    NOX     RM   AGE     DIS  RAD  TAX  \\\n0    0.00632  18.0   2.31     0  0.538  6.575  65.2  4.0900    1  296   \n1    0.02731   0.0   7.07     0  0.469  6.421  78.9  4.9671    2  242   \n2    0.02729   0.0   7.07     0  0.469  7.185  61.1  4.9671    2  242   \n3    0.03237   0.0   2.18     0  0.458  6.998  45.8  6.0622    3  222   \n4    0.06905   0.0   2.18     0  0.458  7.147  54.2  6.0622    3  222   \n..       ...   ...    ...   ...    ...    ...   ...     ...  ...  ...   \n501  0.06263   0.0  11.93     0  0.573  6.593  69.1  2.4786    1  273   \n502  0.04527   0.0  11.93     0  0.573  6.120  76.7  2.2875    1  273   \n503  0.06076   0.0  11.93     0  0.573  6.976  91.0  2.1675    1  273   \n504  0.10959   0.0  11.93     0  0.573  6.794  89.3  2.3889    1  273   \n505  0.04741   0.0  11.93     0  0.573  6.030  80.8  2.5050    1  273   \n\n     PTRATIO       B  LSTAT  MEDV  \n0       15.3  396.90   4.98  24.0  \n1       17.8  396.90   9.14  21.6  \n2       17.8  392.83   4.03  34.7  \n3       18.7  394.63   2.94  33.4  \n4       18.7  396.90   5.33  36.2  \n..       ...     ...    ...   ...  \n501     21.0  391.99   9.67  22.4  \n502     21.0  396.90   9.08  20.6  \n503     21.0  396.90   5.64  23.9  \n504     21.0  393.45   6.48  22.0  \n505     21.0  396.90   7.88  11.9  \n\n[506 rows x 14 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>CRIM</th>\n      <th>ZN</th>\n      <th>INDUS</th>\n      <th>CHAS</th>\n      <th>NOX</th>\n      <th>RM</th>\n      <th>AGE</th>\n      <th>DIS</th>\n      <th>RAD</th>\n      <th>TAX</th>\n      <th>PTRATIO</th>\n      <th>B</th>\n      <th>LSTAT</th>\n      <th>MEDV</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.00632</td>\n      <td>18.0</td>\n      <td>2.31</td>\n      <td>0</td>\n      <td>0.538</td>\n      <td>6.575</td>\n      <td>65.2</td>\n      <td>4.0900</td>\n      <td>1</td>\n      <td>296</td>\n      <td>15.3</td>\n      <td>396.90</td>\n      <td>4.98</td>\n      <td>24.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.02731</td>\n      <td>0.0</td>\n      <td>7.07</td>\n      <td>0</td>\n      <td>0.469</td>\n      <td>6.421</td>\n      <td>78.9</td>\n      <td>4.9671</td>\n      <td>2</td>\n      <td>242</td>\n      <td>17.8</td>\n      <td>396.90</td>\n      <td>9.14</td>\n      <td>21.6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.02729</td>\n      <td>0.0</td>\n      <td>7.07</td>\n      <td>0</td>\n      <td>0.469</td>\n      <td>7.185</td>\n      <td>61.1</td>\n      <td>4.9671</td>\n      <td>2</td>\n      <td>242</td>\n      <td>17.8</td>\n      <td>392.83</td>\n      <td>4.03</td>\n      <td>34.7</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.03237</td>\n      <td>0.0</td>\n      <td>2.18</td>\n      <td>0</td>\n      <td>0.458</td>\n      <td>6.998</td>\n      <td>45.8</td>\n      <td>6.0622</td>\n      <td>3</td>\n      <td>222</td>\n      <td>18.7</td>\n      <td>394.63</td>\n      <td>2.94</td>\n      <td>33.4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.06905</td>\n      <td>0.0</td>\n      <td>2.18</td>\n      <td>0</td>\n      <td>0.458</td>\n      <td>7.147</td>\n      <td>54.2</td>\n      <td>6.0622</td>\n      <td>3</td>\n      <td>222</td>\n      <td>18.7</td>\n      <td>396.90</td>\n      <td>5.33</td>\n      <td>36.2</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>501</th>\n      <td>0.06263</td>\n      <td>0.0</td>\n      <td>11.93</td>\n      <td>0</td>\n      <td>0.573</td>\n      <td>6.593</td>\n      <td>69.1</td>\n      <td>2.4786</td>\n      <td>1</td>\n      <td>273</td>\n      <td>21.0</td>\n      <td>391.99</td>\n      <td>9.67</td>\n      <td>22.4</td>\n    </tr>\n    <tr>\n      <th>502</th>\n      <td>0.04527</td>\n      <td>0.0</td>\n      <td>11.93</td>\n      <td>0</td>\n      <td>0.573</td>\n      <td>6.120</td>\n      <td>76.7</td>\n      <td>2.2875</td>\n      <td>1</td>\n      <td>273</td>\n      <td>21.0</td>\n      <td>396.90</td>\n      <td>9.08</td>\n      <td>20.6</td>\n    </tr>\n    <tr>\n      <th>503</th>\n      <td>0.06076</td>\n      <td>0.0</td>\n      <td>11.93</td>\n      <td>0</td>\n      <td>0.573</td>\n      <td>6.976</td>\n      <td>91.0</td>\n      <td>2.1675</td>\n      <td>1</td>\n      <td>273</td>\n      <td>21.0</td>\n      <td>396.90</td>\n      <td>5.64</td>\n      <td>23.9</td>\n    </tr>\n    <tr>\n      <th>504</th>\n      <td>0.10959</td>\n      <td>0.0</td>\n      <td>11.93</td>\n      <td>0</td>\n      <td>0.573</td>\n      <td>6.794</td>\n      <td>89.3</td>\n      <td>2.3889</td>\n      <td>1</td>\n      <td>273</td>\n      <td>21.0</td>\n      <td>393.45</td>\n      <td>6.48</td>\n      <td>22.0</td>\n    </tr>\n    <tr>\n      <th>505</th>\n      <td>0.04741</td>\n      <td>0.0</td>\n      <td>11.93</td>\n      <td>0</td>\n      <td>0.573</td>\n      <td>6.030</td>\n      <td>80.8</td>\n      <td>2.5050</td>\n      <td>1</td>\n      <td>273</td>\n      <td>21.0</td>\n      <td>396.90</td>\n      <td>7.88</td>\n      <td>11.9</td>\n    </tr>\n  </tbody>\n</table>\n<p>506 rows × 14 columns</p>\n</div>"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.读取数据源\n",
    "df = pd.read_csv(\"../data/house_data.csv\")\n",
    "df\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [
    {
     "data": {
      "text/plain": "        CRIM    ZN  INDUS  CHAS    NOX     RM   AGE     DIS  RAD  TAX  \\\n0    0.00632  18.0   2.31     0  0.538  6.575  65.2  4.0900    1  296   \n1    0.02731   0.0   7.07     0  0.469  6.421  78.9  4.9671    2  242   \n2    0.02729   0.0   7.07     0  0.469  7.185  61.1  4.9671    2  242   \n3    0.03237   0.0   2.18     0  0.458  6.998  45.8  6.0622    3  222   \n4    0.06905   0.0   2.18     0  0.458  7.147  54.2  6.0622    3  222   \n..       ...   ...    ...   ...    ...    ...   ...     ...  ...  ...   \n501  0.06263   0.0  11.93     0  0.573  6.593  69.1  2.4786    1  273   \n502  0.04527   0.0  11.93     0  0.573  6.120  76.7  2.2875    1  273   \n503  0.06076   0.0  11.93     0  0.573  6.976  91.0  2.1675    1  273   \n504  0.10959   0.0  11.93     0  0.573  6.794  89.3  2.3889    1  273   \n505  0.04741   0.0  11.93     0  0.573  6.030  80.8  2.5050    1  273   \n\n     PTRATIO       B  LSTAT  \n0       15.3  396.90   4.98  \n1       17.8  396.90   9.14  \n2       17.8  392.83   4.03  \n3       18.7  394.63   2.94  \n4       18.7  396.90   5.33  \n..       ...     ...    ...  \n501     21.0  391.99   9.67  \n502     21.0  396.90   9.08  \n503     21.0  396.90   5.64  \n504     21.0  393.45   6.48  \n505     21.0  396.90   7.88  \n\n[506 rows x 13 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>CRIM</th>\n      <th>ZN</th>\n      <th>INDUS</th>\n      <th>CHAS</th>\n      <th>NOX</th>\n      <th>RM</th>\n      <th>AGE</th>\n      <th>DIS</th>\n      <th>RAD</th>\n      <th>TAX</th>\n      <th>PTRATIO</th>\n      <th>B</th>\n      <th>LSTAT</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.00632</td>\n      <td>18.0</td>\n      <td>2.31</td>\n      <td>0</td>\n      <td>0.538</td>\n      <td>6.575</td>\n      <td>65.2</td>\n      <td>4.0900</td>\n      <td>1</td>\n      <td>296</td>\n      <td>15.3</td>\n      <td>396.90</td>\n      <td>4.98</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.02731</td>\n      <td>0.0</td>\n      <td>7.07</td>\n      <td>0</td>\n      <td>0.469</td>\n      <td>6.421</td>\n      <td>78.9</td>\n      <td>4.9671</td>\n      <td>2</td>\n      <td>242</td>\n      <td>17.8</td>\n      <td>396.90</td>\n      <td>9.14</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.02729</td>\n      <td>0.0</td>\n      <td>7.07</td>\n      <td>0</td>\n      <td>0.469</td>\n      <td>7.185</td>\n      <td>61.1</td>\n      <td>4.9671</td>\n      <td>2</td>\n      <td>242</td>\n      <td>17.8</td>\n      <td>392.83</td>\n      <td>4.03</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.03237</td>\n      <td>0.0</td>\n      <td>2.18</td>\n      <td>0</td>\n      <td>0.458</td>\n      <td>6.998</td>\n      <td>45.8</td>\n      <td>6.0622</td>\n      <td>3</td>\n      <td>222</td>\n      <td>18.7</td>\n      <td>394.63</td>\n      <td>2.94</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.06905</td>\n      <td>0.0</td>\n      <td>2.18</td>\n      <td>0</td>\n      <td>0.458</td>\n      <td>7.147</td>\n      <td>54.2</td>\n      <td>6.0622</td>\n      <td>3</td>\n      <td>222</td>\n      <td>18.7</td>\n      <td>396.90</td>\n      <td>5.33</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>501</th>\n      <td>0.06263</td>\n      <td>0.0</td>\n      <td>11.93</td>\n      <td>0</td>\n      <td>0.573</td>\n      <td>6.593</td>\n      <td>69.1</td>\n      <td>2.4786</td>\n      <td>1</td>\n      <td>273</td>\n      <td>21.0</td>\n      <td>391.99</td>\n      <td>9.67</td>\n    </tr>\n    <tr>\n      <th>502</th>\n      <td>0.04527</td>\n      <td>0.0</td>\n      <td>11.93</td>\n      <td>0</td>\n      <td>0.573</td>\n      <td>6.120</td>\n      <td>76.7</td>\n      <td>2.2875</td>\n      <td>1</td>\n      <td>273</td>\n      <td>21.0</td>\n      <td>396.90</td>\n      <td>9.08</td>\n    </tr>\n    <tr>\n      <th>503</th>\n      <td>0.06076</td>\n      <td>0.0</td>\n      <td>11.93</td>\n      <td>0</td>\n      <td>0.573</td>\n      <td>6.976</td>\n      <td>91.0</td>\n      <td>2.1675</td>\n      <td>1</td>\n      <td>273</td>\n      <td>21.0</td>\n      <td>396.90</td>\n      <td>5.64</td>\n    </tr>\n    <tr>\n      <th>504</th>\n      <td>0.10959</td>\n      <td>0.0</td>\n      <td>11.93</td>\n      <td>0</td>\n      <td>0.573</td>\n      <td>6.794</td>\n      <td>89.3</td>\n      <td>2.3889</td>\n      <td>1</td>\n      <td>273</td>\n      <td>21.0</td>\n      <td>393.45</td>\n      <td>6.48</td>\n    </tr>\n    <tr>\n      <th>505</th>\n      <td>0.04741</td>\n      <td>0.0</td>\n      <td>11.93</td>\n      <td>0</td>\n      <td>0.573</td>\n      <td>6.030</td>\n      <td>80.8</td>\n      <td>2.5050</td>\n      <td>1</td>\n      <td>273</td>\n      <td>21.0</td>\n      <td>396.90</td>\n      <td>7.88</td>\n    </tr>\n  </tbody>\n</table>\n<p>506 rows × 13 columns</p>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "0      24.0\n1      21.6\n2      34.7\n3      33.4\n4      36.2\n       ... \n501    22.4\n502    20.6\n503    23.9\n504    22.0\n505    11.9\nName: MEDV, Length: 506, dtype: float64"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 2.选择特征和目标\n",
    "X = df.iloc[:,:-1]\n",
    "y = df.MEDV\n",
    "# y = df[\"MEDV\"]\n",
    "display(X,y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "data": {
      "text/plain": "         CRIM    ZN  INDUS  CHAS    NOX     RM   AGE     DIS  RAD  TAX  \\\n478  10.23300   0.0  18.10     0  0.614  6.185  96.7  2.1705   24  666   \n26    0.67191   0.0   8.14     0  0.538  5.813  90.3  4.6820    4  307   \n7     0.14455  12.5   7.87     0  0.524  6.172  96.1  5.9505    5  311   \n492   0.11132   0.0  27.74     0  0.609  5.983  83.5  2.1099    4  711   \n108   0.12802   0.0   8.56     0  0.520  6.474  97.1  2.4329    5  384   \n..        ...   ...    ...   ...    ...    ...   ...     ...  ...  ...   \n106   0.17120   0.0   8.56     0  0.520  5.836  91.9  2.2110    5  384   \n270   0.29916  20.0   6.96     0  0.464  5.856  42.1  4.4290    3  223   \n348   0.01501  80.0   2.01     0  0.435  6.635  29.7  8.3440    4  280   \n435  11.16040   0.0  18.10     0  0.740  6.629  94.6  2.1247   24  666   \n102   0.22876   0.0   8.56     0  0.520  6.405  85.4  2.7147    5  384   \n\n     PTRATIO       B  LSTAT  \n478     20.2  379.70  18.03  \n26      21.0  376.88  14.81  \n7       15.2  396.90  19.15  \n492     20.1  396.90  13.35  \n108     20.9  395.24  12.27  \n..       ...     ...    ...  \n106     20.9  395.67  18.66  \n270     18.6  388.65  13.00  \n348     17.0  390.94   5.99  \n435     20.2  109.85  23.27  \n102     20.9   70.80  10.63  \n\n[339 rows x 13 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>CRIM</th>\n      <th>ZN</th>\n      <th>INDUS</th>\n      <th>CHAS</th>\n      <th>NOX</th>\n      <th>RM</th>\n      <th>AGE</th>\n      <th>DIS</th>\n      <th>RAD</th>\n      <th>TAX</th>\n      <th>PTRATIO</th>\n      <th>B</th>\n      <th>LSTAT</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>478</th>\n      <td>10.23300</td>\n      <td>0.0</td>\n      <td>18.10</td>\n      <td>0</td>\n      <td>0.614</td>\n      <td>6.185</td>\n      <td>96.7</td>\n      <td>2.1705</td>\n      <td>24</td>\n      <td>666</td>\n      <td>20.2</td>\n      <td>379.70</td>\n      <td>18.03</td>\n    </tr>\n    <tr>\n      <th>26</th>\n      <td>0.67191</td>\n      <td>0.0</td>\n      <td>8.14</td>\n      <td>0</td>\n      <td>0.538</td>\n      <td>5.813</td>\n      <td>90.3</td>\n      <td>4.6820</td>\n      <td>4</td>\n      <td>307</td>\n      <td>21.0</td>\n      <td>376.88</td>\n      <td>14.81</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>0.14455</td>\n      <td>12.5</td>\n      <td>7.87</td>\n      <td>0</td>\n      <td>0.524</td>\n      <td>6.172</td>\n      <td>96.1</td>\n      <td>5.9505</td>\n      <td>5</td>\n      <td>311</td>\n      <td>15.2</td>\n      <td>396.90</td>\n      <td>19.15</td>\n    </tr>\n    <tr>\n      <th>492</th>\n      <td>0.11132</td>\n      <td>0.0</td>\n      <td>27.74</td>\n      <td>0</td>\n      <td>0.609</td>\n      <td>5.983</td>\n      <td>83.5</td>\n      <td>2.1099</td>\n      <td>4</td>\n      <td>711</td>\n      <td>20.1</td>\n      <td>396.90</td>\n      <td>13.35</td>\n    </tr>\n    <tr>\n      <th>108</th>\n      <td>0.12802</td>\n      <td>0.0</td>\n      <td>8.56</td>\n      <td>0</td>\n      <td>0.520</td>\n      <td>6.474</td>\n      <td>97.1</td>\n      <td>2.4329</td>\n      <td>5</td>\n      <td>384</td>\n      <td>20.9</td>\n      <td>395.24</td>\n      <td>12.27</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>106</th>\n      <td>0.17120</td>\n      <td>0.0</td>\n      <td>8.56</td>\n      <td>0</td>\n      <td>0.520</td>\n      <td>5.836</td>\n      <td>91.9</td>\n      <td>2.2110</td>\n      <td>5</td>\n      <td>384</td>\n      <td>20.9</td>\n      <td>395.67</td>\n      <td>18.66</td>\n    </tr>\n    <tr>\n      <th>270</th>\n      <td>0.29916</td>\n      <td>20.0</td>\n      <td>6.96</td>\n      <td>0</td>\n      <td>0.464</td>\n      <td>5.856</td>\n      <td>42.1</td>\n      <td>4.4290</td>\n      <td>3</td>\n      <td>223</td>\n      <td>18.6</td>\n      <td>388.65</td>\n      <td>13.00</td>\n    </tr>\n    <tr>\n      <th>348</th>\n      <td>0.01501</td>\n      <td>80.0</td>\n      <td>2.01</td>\n      <td>0</td>\n      <td>0.435</td>\n      <td>6.635</td>\n      <td>29.7</td>\n      <td>8.3440</td>\n      <td>4</td>\n      <td>280</td>\n      <td>17.0</td>\n      <td>390.94</td>\n      <td>5.99</td>\n    </tr>\n    <tr>\n      <th>435</th>\n      <td>11.16040</td>\n      <td>0.0</td>\n      <td>18.10</td>\n      <td>0</td>\n      <td>0.740</td>\n      <td>6.629</td>\n      <td>94.6</td>\n      <td>2.1247</td>\n      <td>24</td>\n      <td>666</td>\n      <td>20.2</td>\n      <td>109.85</td>\n      <td>23.27</td>\n    </tr>\n    <tr>\n      <th>102</th>\n      <td>0.22876</td>\n      <td>0.0</td>\n      <td>8.56</td>\n      <td>0</td>\n      <td>0.520</td>\n      <td>6.405</td>\n      <td>85.4</td>\n      <td>2.7147</td>\n      <td>5</td>\n      <td>384</td>\n      <td>20.9</td>\n      <td>70.80</td>\n      <td>10.63</td>\n    </tr>\n  </tbody>\n</table>\n<p>339 rows × 13 columns</p>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "        CRIM    ZN  INDUS  CHAS    NOX     RM   AGE     DIS  RAD  TAX  \\\n173  0.09178   0.0   4.05     0  0.510  6.416  84.1  2.6463    5  296   \n274  0.05644  40.0   6.41     1  0.447  6.758  32.9  4.0776    4  254   \n491  0.10574   0.0  27.74     0  0.609  5.983  98.8  1.8681    4  711   \n72   0.09164   0.0  10.81     0  0.413  6.065   7.8  5.2873    4  305   \n452  5.09017   0.0  18.10     0  0.713  6.297  91.8  2.3682   24  666   \n..       ...   ...    ...   ...    ...    ...   ...     ...  ...  ...   \n110  0.10793   0.0   8.56     0  0.520  6.195  54.4  2.7778    5  384   \n321  0.18159   0.0   7.38     0  0.493  6.376  54.3  4.5404    5  287   \n265  0.76162  20.0   3.97     0  0.647  5.560  62.8  1.9865    5  264   \n29   1.00245   0.0   8.14     0  0.538  6.674  87.3  4.2390    4  307   \n262  0.52014  20.0   3.97     0  0.647  8.398  91.5  2.2885    5  264   \n\n     PTRATIO       B  LSTAT  \n173     16.6  395.50   9.04  \n274     17.6  396.90   3.53  \n491     20.1  390.11  18.07  \n72      19.2  390.91   5.52  \n452     20.2  385.09  17.27  \n..       ...     ...    ...  \n110     20.9  393.49  13.00  \n321     19.6  396.90   6.87  \n265     13.0  392.40  10.45  \n29      21.0  380.23  11.98  \n262     13.0  386.86   5.91  \n\n[167 rows x 13 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>CRIM</th>\n      <th>ZN</th>\n      <th>INDUS</th>\n      <th>CHAS</th>\n      <th>NOX</th>\n      <th>RM</th>\n      <th>AGE</th>\n      <th>DIS</th>\n      <th>RAD</th>\n      <th>TAX</th>\n      <th>PTRATIO</th>\n      <th>B</th>\n      <th>LSTAT</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>173</th>\n      <td>0.09178</td>\n      <td>0.0</td>\n      <td>4.05</td>\n      <td>0</td>\n      <td>0.510</td>\n      <td>6.416</td>\n      <td>84.1</td>\n      <td>2.6463</td>\n      <td>5</td>\n      <td>296</td>\n      <td>16.6</td>\n      <td>395.50</td>\n      <td>9.04</td>\n    </tr>\n    <tr>\n      <th>274</th>\n      <td>0.05644</td>\n      <td>40.0</td>\n      <td>6.41</td>\n      <td>1</td>\n      <td>0.447</td>\n      <td>6.758</td>\n      <td>32.9</td>\n      <td>4.0776</td>\n      <td>4</td>\n      <td>254</td>\n      <td>17.6</td>\n      <td>396.90</td>\n      <td>3.53</td>\n    </tr>\n    <tr>\n      <th>491</th>\n      <td>0.10574</td>\n      <td>0.0</td>\n      <td>27.74</td>\n      <td>0</td>\n      <td>0.609</td>\n      <td>5.983</td>\n      <td>98.8</td>\n      <td>1.8681</td>\n      <td>4</td>\n      <td>711</td>\n      <td>20.1</td>\n      <td>390.11</td>\n      <td>18.07</td>\n    </tr>\n    <tr>\n      <th>72</th>\n      <td>0.09164</td>\n      <td>0.0</td>\n      <td>10.81</td>\n      <td>0</td>\n      <td>0.413</td>\n      <td>6.065</td>\n      <td>7.8</td>\n      <td>5.2873</td>\n      <td>4</td>\n      <td>305</td>\n      <td>19.2</td>\n      <td>390.91</td>\n      <td>5.52</td>\n    </tr>\n    <tr>\n      <th>452</th>\n      <td>5.09017</td>\n      <td>0.0</td>\n      <td>18.10</td>\n      <td>0</td>\n      <td>0.713</td>\n      <td>6.297</td>\n      <td>91.8</td>\n      <td>2.3682</td>\n      <td>24</td>\n      <td>666</td>\n      <td>20.2</td>\n      <td>385.09</td>\n      <td>17.27</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>110</th>\n      <td>0.10793</td>\n      <td>0.0</td>\n      <td>8.56</td>\n      <td>0</td>\n      <td>0.520</td>\n      <td>6.195</td>\n      <td>54.4</td>\n      <td>2.7778</td>\n      <td>5</td>\n      <td>384</td>\n      <td>20.9</td>\n      <td>393.49</td>\n      <td>13.00</td>\n    </tr>\n    <tr>\n      <th>321</th>\n      <td>0.18159</td>\n      <td>0.0</td>\n      <td>7.38</td>\n      <td>0</td>\n      <td>0.493</td>\n      <td>6.376</td>\n      <td>54.3</td>\n      <td>4.5404</td>\n      <td>5</td>\n      <td>287</td>\n      <td>19.6</td>\n      <td>396.90</td>\n      <td>6.87</td>\n    </tr>\n    <tr>\n      <th>265</th>\n      <td>0.76162</td>\n      <td>20.0</td>\n      <td>3.97</td>\n      <td>0</td>\n      <td>0.647</td>\n      <td>5.560</td>\n      <td>62.8</td>\n      <td>1.9865</td>\n      <td>5</td>\n      <td>264</td>\n      <td>13.0</td>\n      <td>392.40</td>\n      <td>10.45</td>\n    </tr>\n    <tr>\n      <th>29</th>\n      <td>1.00245</td>\n      <td>0.0</td>\n      <td>8.14</td>\n      <td>0</td>\n      <td>0.538</td>\n      <td>6.674</td>\n      <td>87.3</td>\n      <td>4.2390</td>\n      <td>4</td>\n      <td>307</td>\n      <td>21.0</td>\n      <td>380.23</td>\n      <td>11.98</td>\n    </tr>\n    <tr>\n      <th>262</th>\n      <td>0.52014</td>\n      <td>20.0</td>\n      <td>3.97</td>\n      <td>0</td>\n      <td>0.647</td>\n      <td>8.398</td>\n      <td>91.5</td>\n      <td>2.2885</td>\n      <td>5</td>\n      <td>264</td>\n      <td>13.0</td>\n      <td>386.86</td>\n      <td>5.91</td>\n    </tr>\n  </tbody>\n</table>\n<p>167 rows × 13 columns</p>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 3.拆分数据集\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.33, random_state=42)\n",
    "display(X_train,X_test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0.89624872, -0.51060139,  0.98278223, ...,  0.86442095,\n         0.24040357,  0.77155612],\n       [-0.34895881, -0.51060139, -0.44867555, ...,  1.22118698,\n         0.20852839,  0.32248963],\n       [-0.41764058,  0.03413008, -0.48748013, ..., -1.36536677,\n         0.43481957,  0.92775316],\n       ...,\n       [-0.43451148,  2.97567999, -1.32968321, ..., -0.56264319,\n         0.36745216, -0.90756208],\n       [ 1.01703049, -0.51060139,  0.98278223, ...,  0.86442095,\n        -2.80977992,  1.50233514],\n       [-0.40667333, -0.51060139, -0.38831288, ...,  1.17659123,\n        -3.25117205, -0.26046005]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([[-0.42451319, -0.51060139, -1.03649306, ..., -0.74102621,\n         0.41899501, -0.48220406],\n       [-0.42911576,  1.2325393 , -0.6973123 , ..., -0.29506866,\n         0.43481957, -1.25063772],\n       [-0.42269508, -0.51060139,  2.36824941, ...,  0.8198252 ,\n         0.35807046,  0.77713459],\n       ...,\n       [-0.33727525,  0.36096896, -1.04799071, ..., -2.34647337,\n         0.38395492, -0.28556314],\n       [-0.30591027, -0.51060139, -0.44867555, ...,  1.22118698,\n         0.2463943 , -0.07218683],\n       [-0.36872487,  0.36096896, -1.04799071, ..., -2.34647337,\n         0.32133488, -0.91871901]])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 4.无量纲化--标准化\n",
    "scaler = StandardScaler()\n",
    "X_train = scaler.fit_transform(X_train)\n",
    "X_test = scaler.transform(X_test)\n",
    "display(X_train,X_test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "data": {
      "text/plain": "22.970796460176988"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# y = df[[\"MEDV\"]]\n",
    "# display(y)# 原来的值\n",
    "# sdy  = StandardScaler()\n",
    "# y1 = sdy.fit_transform(y)\n",
    "# display(y1) # 做完标准化的值\n",
    "# y_test1 = sdy.inverse_transform(y1)\n",
    "# display(y_test1) # 回到原来的值\n",
    "\n",
    "\n",
    "# 5.训练模型\n",
    "lr = LinearRegression()\n",
    "lr.fit(X_train,y_train)\n",
    "lr.coef_\n",
    "lr.intercept_"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "data": {
      "text/plain": "array([28.53469469, 36.6187006 , 15.63751079, 25.5014496 , 18.7096734 ,\n       23.16471591, 17.31011035, 14.07736367, 23.01064388, 20.54223482,\n       24.91632351, 18.41098052, -6.52079687, 21.83372604, 19.14903064,\n       26.0587322 , 20.30232625,  5.74943567, 40.33137811, 17.45791446,\n       27.47486665, 30.2170757 , 10.80555625, 23.87721728, 17.99492211,\n       16.02608791, 23.268288  , 14.36825207, 22.38116971, 19.3092068 ,\n       22.17284576, 25.05925441, 25.13780726, 18.46730198, 16.60405712,\n       17.46564046, 30.71367733, 20.05106788, 23.9897768 , 24.94322408,\n       13.97945355, 31.64706967, 42.48057206, 17.70042814, 26.92507869,\n       17.15897719, 13.68918087, 26.14924245, 20.2782306 , 29.99003492,\n       21.21260347, 34.03649185, 15.41837553, 25.95781061, 39.13897274,\n       22.96118424, 18.80310558, 33.07865362, 24.74384155, 12.83640958,\n       22.41963398, 30.64804979, 31.59567111, 16.34088197, 20.9504304 ,\n       16.70145875, 20.23215646, 26.1437865 , 31.12160889, 11.89762768,\n       20.45432404, 27.48356359, 10.89034224, 16.77707214, 24.02593714,\n        5.44691807, 21.35152331, 41.27267175, 18.13447647,  9.8012101 ,\n       21.24024342, 13.02644969, 21.80198374,  9.48201752, 22.99183857,\n       31.90465631, 18.95594718, 25.48515032, 29.49687019, 20.07282539,\n       25.5616062 ,  5.59584382, 20.18410904, 15.08773299, 14.34562117,\n       20.85155407, 24.80149389, -0.19785401, 13.57649004, 15.64401679,\n       22.03765773, 24.70314482, 10.86409112, 19.60231067, 23.73429161,\n       12.08082177, 18.40997903, 25.4366158 , 20.76506636, 24.68588237,\n        7.4995836 , 18.93015665, 21.70801764, 27.14350579, 31.93765208,\n       15.19483586, 34.01357428, 12.85763091, 21.06646184, 28.58470042,\n       15.77437534, 24.77512495,  3.64655689, 23.91169589, 25.82292925,\n       23.03339677, 25.35158335, 33.05655447, 20.65930467, 38.18917361,\n       14.04714297, 25.26034469, 17.6138723 , 20.60883766,  9.8525544 ,\n       21.06756951, 22.20145587, 32.2920276 , 31.57638342, 15.29265938,\n       16.7100235 , 29.10550932, 25.17762329, 16.88159225,  6.32621877,\n       26.70210263, 23.3525851 , 17.24168182, 13.22815696, 39.49907507,\n       16.53528575, 18.14635902, 25.06620426, 23.70640231, 22.20167772,\n       21.22272327, 16.89825921, 23.15518273, 28.69699805,  6.65526482,\n       23.98399958, 17.21004545, 21.0574427 , 25.01734597, 27.65461859,\n       20.70205823, 40.38214871])"
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 6.评估模型\n",
    "y_pred = lr.predict(X_test)\n",
    "y_pred\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20.72402343733975\n"
     ]
    }
   ],
   "source": [
    "result = mean_squared_error(y_test,y_pred)\n",
    "print(result)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [
    {
     "data": {
      "text/plain": "array([22.97152461])"
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  梯度下降\n",
    "lr = SGDRegressor(max_iter=10000,eta0=0.02)\n",
    "lr.fit(X_train,y_train)\n",
    "lr.coef_\n",
    "lr.intercept_"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21.021977727976775\n"
     ]
    }
   ],
   "source": [
    "y_pred1 = lr.predict(X_test)\n",
    "y_pred1\n",
    "result = mean_squared_error(y_test,y_pred1)\n",
    "print(result)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}